package com.neusoft.elm.service.Impl;

import com.neusoft.elm.dao.CartDao;
import com.neusoft.elm.dao.impl.CartDaoImpl;
import com.neusoft.elm.po.Cart;
import com.neusoft.elm.service.CartService;
import com.neusoft.elm.util.DBUtil;


import java.util.ArrayList;
import java.util.List;

/**
 * 购物车方法实现
 * Service
 */
public class CartServiceImpl implements CartService {

    /**
     * 新增购物车
     *
     * @param cart
     * @return
     */
    @Override
    public int saveCart(Cart cart) {
        int result = 0;
        CartDao dao = new CartDaoImpl();
        try{
            DBUtil.getConnection();
            result = dao.saveCart(cart);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return result;
    }

    /**
     * 更新购物车
     *
     * @param cart
     * @return
     */
    @Override
    public int updateCart(Cart cart) {
        int result = 0;
        CartDao dao = new CartDaoImpl();
        try{
            DBUtil.getConnection();
            result = dao.updateCart(cart);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return result;
    }

    /**
     * 删除购物车
     *
     * @param cart
     * @return
     */
    @Override
    public int removeCart(Cart cart) {
        int result = 0;
        CartDao dao = new CartDaoImpl();
        try{
            DBUtil.getConnection();
            result = dao.removeCart(cart);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return result;
    }

    /**
     * 列出购物车信息
     *
     * @param cart
     * @return
     */
    @Override
    public List<Cart> listCart(Cart cart) {
        List<Cart> list = new ArrayList<>();
        CartDao dao = new CartDaoImpl();
        try {
            DBUtil.getConnection();
            list = dao.listCart(cart);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;
    }
}
